﻿using Rubyer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;



namespace WpfSnqkGasAnalysis
{
    /// <summary>
    /// 消息框封装，消息2秒后消失
    /// </summary>
    /// 创建时间：2022年11月30日15:08:25。作者：王浩力
    public static class ShowAlertHelpter
    {
         
        //上一次显示的top位置
        static double lastTop = 0;
        static double lastLeft = 0;

        //上一次显示时间
        static DateTime lastShowTime = DateTime.Now;

        /// <summary>
        /// 提示消息。消息2秒后消失
        /// </summary>
        /// <param name="msg">消息</param>        
        /// <param name="alwaysShow">是否一直显示</param>
        public static void ShowAlert(string msg,  bool alwaysShow = false)
        {
            if (string.IsNullOrWhiteSpace(msg))
            {
                return;
            }

            //Grid grid = Application.Current.Windows[0].FindName("mainGrid") as Grid;
            //Grid grid = null;
            //if (page is Window)
            //{
            //    Window window = (Window)page;
            //    grid = window.Content as Grid;
            //}
            //else if (page is Page)
            //{
            //    Page args = (Page)page;
            //    grid = args.Content as Grid;
            //}
            //else if (page is Grid)
            //{
            //    grid = page as Grid;
            //}

            Application.Current.Dispatcher.BeginInvoke(
               () =>
            {
                Window? win = Application.Current.Windows[0];
                //Grid grid = win?.Content as Grid;
                Grid grid = win.TryGetChildPartFromVisualTree<Grid>();
                if (grid == null)
                {
                    return;
                }

                //Popup使消息在最前面显示
                Popup popup = new Popup();
                popup.Placement = PlacementMode.Top;//显示位置
                popup.PopupAnimation = PopupAnimation.Fade;//渐入
                popup.AllowsTransparency = true;//设置Popup透明
                popup.Margin = new Thickness(20);
                popup.VerticalOffset = 200;
                popup.HorizontalOffset = 600;
                popup.HorizontalAlignment = HorizontalAlignment.Center;
                //popup.IsOpen = false;
                popup.IsOpen = true;
                popup.ToolTip = "点击关闭";

                TextBlock textBlock = new TextBlock();
                textBlock.Text = msg;
                textBlock.Style = (System.Windows.Style)grid.FindResource("TextBlock_Alert");

                //边框阴影
                System.Windows.Media.Effects.DropShadowEffect effect = new System.Windows.Media.Effects.DropShadowEffect();
                effect.BlurRadius = 20;
                effect.Color = Color.FromRgb(158, 158, 158);
                textBlock.Effect = effect;

                if (lastTop > 0 && (DateTime.Now - lastShowTime).TotalSeconds < 3)
                {
                    //字符多，增加偏移
                    double space = 50d;
                    if (msg.Length > 22)
                    {
                        space = 70d;
                    }
                    popup.HorizontalOffset = lastLeft + space;
                    popup.VerticalOffset = lastTop + space;
                }

                //一直显示消息
                if (alwaysShow || msg.Contains("失败") || msg.Contains("异常") || msg.Contains("出错")
               || msg.Contains("error")
                )
                {
                    textBlock.Background = Brushes.Orchid;//红色背景
                    goto SKIP_END;
                }

                //消息提示自动消失
                DispatcherTimer timer = new DispatcherTimer();
                timer.Interval = TimeSpan.FromMilliseconds(2000);
                timer.Tick += (s, e) =>
                {
                    grid.Children.Remove(popup);
                    timer.Stop();

                    if (lastTop > 500)
                    {
                        lastTop = 0;
                        lastLeft = -200;
                    }
                };
                timer.Start();

            SKIP_END:
                lastTop = popup.VerticalOffset;
                lastLeft = popup.HorizontalOffset;
                lastShowTime = DateTime.Now;

                popup.Child = textBlock;
                grid.Children.Add(popup);

                //点击就移除
                popup.MouseLeftButtonUp += (s, e) =>
                {
                    grid.Children.Remove(popup);
                };

            });
        }

    }
}
